import swaggerJsDoc from 'swagger-jsdoc';
import { Express } from 'express';
import swaggerUi from 'swagger-ui-express';
import config from './index';

const swaggerOptions: swaggerJsDoc.Options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'DevOps SmartBot API',
      version: '1.0.0',
      description: 'Intelligent DevOps Alert & Ticket Management System with AI-powered analysis',
      contact: {
        name: 'DevOps SmartBot Team',
        email: 'support@devops-smartbot.com',
      },
      license: {
        name: 'MIT',
        url: 'https://opensource.org/licenses/MIT',
      },
    },
    servers: [
      {
        url: `http://localhost:${config.env.PORT}${config.env.API_PREFIX}`,
        description: 'Development server',
      },
      {
        url: `http://localhost:8080${config.env.API_PREFIX}`,
        description: 'API Gateway (Production)',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
          description: 'Enter your JWT token in the format: Bearer {token}',
        },
      },
      schemas: {
        Error: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              example: false,
            },
            message: {
              type: 'string',
              example: 'Error message description',
            },
            error: {
              type: 'string',
              example: 'Detailed error information',
            },
          },
        },
        SuccessResponse: {
          type: 'object',
          properties: {
            success: {
              type: 'boolean',
              example: true,
            },
            message: {
              type: 'string',
              example: 'Operation completed successfully',
            },
          },
        },
        Pagination: {
          type: 'object',
          properties: {
            current: {
              type: 'number',
              example: 1,
            },
            pageSize: {
              type: 'number',
              example: 10,
            },
            total: {
              type: 'number',
              example: 100,
            },
            totalPages: {
              type: 'number',
              example: 10,
            },
          },
        },
      },
    },
    tags: [
      {
        name: 'Authentication',
        description: 'User authentication and authorization endpoints',
      },
      {
        name: 'Alerts',
        description: 'Alert management and monitoring',
      },
      {
        name: 'Tickets',
        description: 'Ticket creation, management, and lifecycle',
      },
      {
        name: 'Knowledge Base',
        description: 'Knowledge base articles and documentation',
      },
      {
        name: 'Analytics',
        description: 'Analytics and reporting endpoints',
      },
      {
        name: 'Dashboard',
        description: 'Dashboard overview and statistics',
      },
      {
        name: 'System',
        description: 'System configuration and health checks',
      },
    ],
  },
  apis: [
    './src/routes/*.ts',
    './src/routes/*.routes.ts',
    './src/controllers/*.ts',
    './src/controllers/*.controller.ts',
    './src/app.ts',
  ],
};

const swaggerSpec = swaggerJsDoc(swaggerOptions);

export function setupSwagger(app: Express): void {
  // Swagger JSON endpoint
  app.get(`${config.env.API_PREFIX}/swagger.json`, (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    res.send(swaggerSpec);
  });

  // Swagger UI
  app.use(
    `${config.env.API_PREFIX}/docs`,
    swaggerUi.serve,
    swaggerUi.setup(swaggerSpec, {
      explorer: true,
      customCss: '.swagger-ui .topbar { display: none }',
      customSiteTitle: 'DevOps SmartBot API Documentation',
    })
  );
}

export default swaggerSpec;
